A migration is implemented as a class which derives from Migration. The actions to take are specified in the class’ overrides of the Migration.Up and Migration.Down methods.
The Migration class provides a number of methods that you can call to specify the actions to take in the migration. This section lists a few of the key methods; see the Migration class documentation for specific overloads, more details and other methods.
Migration Method | Action | Typical SQL Equivalent |
---|---|---|
AddTable | Adds a new table to the database | CREATE TABLE |
DropTable | Drops a table from the database | DROP TABLE |
RenameTable | Changes the name of an existing table | (varies) |
AddColumn | Adds a column to a table | ALTER TABLE ADD |
AddForeignKeyColumn | Adds a column with a foreign key constraint to a table | ALTER TABLE ADD |
DropColumn | Drops a column from a table | ALTER TABLE DROP |
ChangeColumn | Change the data type or nullability of a column | ALTER TABLE ALTER |
RenameColumn | Changes the name of a column | (varies) |
(SQL equivalents are approximate; the exact SQL syntax varies from database to database.)
Data Types
You will normally specify data types using the ModelDataType class. This abstracts your code from the database representation. For example, you would write ModelDataType.String rather than VARCHAR, NVARCHAR or VARCHAR2.
When you require fine control over data types, you can instead specify a literal string. For example, if you want a string to be represented as a fixed-size field, you could specify “CHAR” instead of ModelDataType.String. Such data types are database specific.
Identity Generation
The Migration class also provides APIs to create the database resources used by the HiLo identity method. See the AddKeyTable API.